#ifndef __UTILS_NUMERIC_H__
#define __UTILS_NUMERIC_H__

#include <cstdlib>

/** Returns true, if requested bit is set, or false otherwise.
 * @param variable variable being tested
 * @param position position of requested bit in variable
 */
template <typename T> bool is_bit_set(T variable, unsigned int position)
{
	T mask = 1;
	mask = mask << position;
	return (variable & mask) != 0;
}

/** Specialisation for double - performs a cast to unsigned int before check.
 */
template <> inline bool is_bit_set(double variable, unsigned int position)
{
	unsigned int a = static_cast<unsigned int>(variable);
	return is_bit_set(a, position);
}

/** Sets requested bit in variable.
 * @param variable reference to modified variable
 * @param position position of bit to be set
 */
template <typename T> void set_bit(T& variable, unsigned int position)
{
	T mask = 1;
	mask = mask << position;
	variable = variable | mask;
}

/** Clears requested bit in variable.
 * @param variable reference to modified variable
 * @param position position of bit to be cleared
 */
template <typename T> void clear_bit(T& variable, unsigned int position)
{
	T mask = 1;
	mask = mask << position;
	variable = variable & ~mask;
}

#endif

